/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __COM_UTIL_LOCK_COUNTER__
#define __COM_UTIL_LOCK_COUNTER__

#include <NoCopy.h>

namespace libcom
{

class LockCounter : private Nocopy
{
public:
	LockCounter()
		:	lock_count(0)
	{
	}
	
	virtual ~LockCounter()
	{
	}

	void lock()
	{
		++lock_count;
	}
	
	void unlock()
	{
		--lock_count;
	}
	
	bool locked()const
	{
		return lock_count != 0;
	}
	
private:
	unsigned long lock_count;
};



class LockObject : private Nocopy
{
public:

	LockObject(LockCounter * counter)
		:	counter(counter)
	{
		counter->lock();
	}
	
	~LockObject()
	{
		counter->unlock();
	}
	
private:
	LockCounter * counter;
};

}//end of namespace

#endif

